package com.googlecode.mgwt.examples.showcase.server;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.jdo.PersistenceManager;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.google.gwt.view.client.DefaultSelectionEventManager.EventTranslator;
import com.googlecode.mgwt.examples.showcase.client.GreetingService;
import com.googlecode.mgwt.examples.showcase.persist.PMF;
import com.googlecode.mgwt.examples.showcase.server.jdo.ServiceCall;
import com.googlecode.mgwt.examples.showcase.shared.ServiceCallDTO;
import com.googlecode.mgwt.examples.showcase.shared.ServiceCallTranslator;
import com.googlecode.mgwt.examples.showcase.shared.exception.ElementExistsException;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class GreetingServiceImpl extends RemoteServiceServlet implements GreetingService {

	

	/**
	 * Escape an html string. Escaping data received from the client helps to
	 * prevent cross-site script vulnerabilities.
	 * 
	 * @param html the html string to escape
	 * @return the escaped string
	 */
	private String escapeHtml(String html) {
		if (html == null) {
			return null;
		}
		return html.replaceAll("&", "&amp;").replaceAll("<", "&lt;")
				.replaceAll(">", "&gt;");
	}

	@Override
	public void createServiceCall(ServiceCallDTO serviceCall) throws ElementExistsException{
		ServiceCall call = ServiceCallTranslator.dto2entity(serviceCall);
		
			
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			String query = "select from " + ServiceCall.class.getName();
			List<ServiceCall> allCalls = (List<ServiceCall>) pm.newQuery(query).execute();
			for (ServiceCall serviceCall2 : allCalls) {
				if(call.equals(serviceCall2)){
					throw new ElementExistsException(ServiceCallTranslator.entity2dto(serviceCall2));
				}
			}
			pm.makePersistent(call);
		} finally {
			pm.close();
		}
	}

	@Override
	public List<ServiceCallDTO> getAllCalls() {
		List<ServiceCallDTO> ret = new ArrayList<ServiceCallDTO>();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try{
			String query = "select from " + ServiceCall.class.getName();
			List<ServiceCall> calls = (List<ServiceCall>) pm.newQuery(query).execute();
		for (ServiceCall diaryEvent : calls) {
				ret.add(ServiceCallTranslator.entity2dto(diaryEvent));
		}
		Collections.sort(ret);//sort by date
		return ret;
		}finally{
			pm.close();
		}
	}

	@Override
	public void remoceCall(long serviceCallID) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try{
			String query = "select from " + ServiceCall.class.getName();
			List<ServiceCall> events = (List<ServiceCall>) pm.newQuery(query).execute();
			for (ServiceCall call : events) {
				if(call.getKey().getId() == serviceCallID){
					pm.deletePersistent(call);

				}
			}
		}finally{
			pm.close();
		}
	}
}
